<template>
    <div class="index-lists">
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="" prop="desc">
                    <el-input class="w-[280px]" v-model="queryParams.title" placeholder="请输入关键字"/>
                </el-form-item>
                <el-form-item label="" prop="type">
                    <el-select
                        v-model="queryParams.type"
                        class="w-[280px]"
                        clearable
                        placeholder="选择文章类型"
                    >
                        <el-option v-for="(item, index) in dictData.article_type" :key="index" :label="item.name" :value="item.value"/>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                    <el-button v-perms="['article:add']" type="primary" @click="handleAdd()">
                        <template #icon>
                            <icon name="el-icon-Plus" />
                        </template>
                        新增
                    </el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <el-table
                class="mt-4"
                size="large"
                v-loading="pager.loading"
                :data="pager.lists"
            >
                <el-table-column label="排序" prop="sort" min-width="50" />
                <el-table-column label="文章标题" prop="title" min-width="150" />
                <el-table-column label="是否展示" prop="isShow" min-width="100" >
                    <template #default="{ row }">
                        <el-switch v-perms="['case_manage:audit:disable']"
                                   :model-value="row.isShow"
                                   :active-value="1"
                                   :inactive-value="0"
                                   @change="changeStatus($event, row.id)"/>
                    </template>
                </el-table-column>
                <el-table-column label="类型" prop="type" min-width="100" >
                    <template #default="{ row }">
                        <dict-value :options="dictData.article_type" :value="row.type" />
                    </template>
                </el-table-column>

                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['article:edit']"
                            type="primary"
                            link
                            @click="handleEdit(row)"
                        >
                            编辑
                        </el-button>
                        <el-button
                            v-perms="['article:del']"
                            type="danger"
                            link
                            @click="handleDelete(row.id)"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup
            v-if="showEdit"
            ref="editRef"
            @success="getLists"
            @close="showEdit = false"
        />
    </div>
</template>
<script lang="ts" setup name="article">
import { articleDelete, articleLists } from '@/api/legalArticle'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import {useDictData} from "@/hooks/useDictOptions";
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
    title: '',
    content: '',
    type: '',
    mainPic: '',
    mainPicThumb: '',
    deletedAtStart: '',
    deletedAtEnd: '',
    createdBy: '',
    createdAtStart: '',
    createdAtEnd: '',
    isShow: '',
    sort: '',
    desc: '',
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: articleLists,
    params: queryParams
})

const { dictData } = useDictData<{
    article_type: any[],
    yes1_no0: any[],
}>(['article_type', 'yes1_no0'])

const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await articleDelete({ id })
    feedback.msgSuccess('删除成功')
    getLists()
}

getLists()
</script>
